package com.sx.core.mybatis;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class ReflectHelper {

	public static Object isPage(Object obj, String fieldName) {
		if (obj instanceof Map) {
			Map map = (Map) obj;
			return map.get(fieldName);
		} else {
			return getFieldByFieldName(obj, fieldName);
		}
	}
	public static List<Field> getFields(Class<?> cls){
		List<Field> fields=new ArrayList<Field>();
		Field[] fs=cls.getDeclaredFields();
		fields.addAll(Arrays.asList(fs));
		Class<?> parent = cls.getSuperclass();
		if(parent!=null && parent.getSimpleName().equalsIgnoreCase("object")){
			return fields;
		}
		fields.addAll(getFields(parent));
		return fields;
	}
	public static Field getField(Class<?> cls,String name){
		Field f=null;
		try {
			f=cls.getDeclaredField(name);
		} catch (NoSuchFieldException e) {
			f=null;
		}
		if(f!=null){
			return f;
		}
		Class<?> parent = cls.getSuperclass();
		if(parent!=null && parent.getSimpleName().equalsIgnoreCase("object")){
			return f;
		}
		return f=getField(parent,name);
	}

	public static Field getFieldByFieldName(Object obj, String fieldName) {
		for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
				;
			}
		}
		return null;
	}

	public static Object getValueByFieldName(Object obj, String fieldName) throws Exception {
		Field field = getFieldByFieldName(obj, fieldName);
		Object value = null;
		if (field == null) {
			return value;
		}
		if (field.isAccessible()) {
			value = field.get(obj);
		} else {
			field.setAccessible(true);
			value = field.get(obj);
			field.setAccessible(false);
		}
		return value;
	}

	public static void setValueByFieldName(Object obj, String fieldName, Object value) throws Exception {
		if (obj instanceof Map) {
			Map map = (Map) obj;
			map.put(fieldName, value);
			return;
		}
		Field field = obj.getClass().getDeclaredField(fieldName);
		if (field.isAccessible()) {
			field.set(obj, value);
		} else {
			field.setAccessible(true);
			field.set(obj, value);
			field.setAccessible(false);
		}
	}
}
